package javabase.schoolwork.day6_1to3;

import java.util.Arrays;
import java.util.List;

/**
 * @author fance
 * @date 2018/6/1 21:50
 */
public class MyTime {
    private int hour;
    private int minute;
    private int second;

    public MyTime() {
    }

    public MyTime(int hour, int minute, int second) {
        this.hour = hour;
        this.minute = minute;
        this.second = second;
    }


    public String display() {
        return "MyTime{" +
                "hour='" + hour + '\'' +
                ", minute='" + minute + '\'' +
                ", second='" + second + '\'' +
                '}';
    }


    public boolean subHour(int hour) {
        if (this.hour < hour) {
            return false;
        }
        this.hour -= hour;
        return true;
    }

    public void addHour(int hour) {
        this.hour += hour;
        List<Integer> res = judge(this.hour,this.minute,this.second);
        this.hour = res.get(0);
        this.minute = res.get(1);
        this.second = res.get(2);
    }

    public boolean subMinute(int minute) {
        if (this.minute < minute) {
            return false;
        }
        this.minute -= minute;
        return true;
    }

    public void addMinute(int minute) {
        this.minute += minute;
        List<Integer> res = judge(this.hour,this.minute,this.second);
        this.hour = res.get(0);
        this.minute = res.get(1);
        this.second = res.get(2);
    }

    public boolean subSecond(int second) {
        if (this.second < second) {
            return false;
        }
        this.second -= second;
        return true;
    }

    public void addSecond(int second) {
        this.second += second;
        List<Integer> res = judge(this.hour,this.minute,this.second);
        this.hour = res.get(0);
        this.minute = res.get(1);
        this.second = res.get(2);

    }

    @Override
    public String toString() {
        return "MyTime{" +
                "hour=" + hour +
                ", minute=" + minute +
                ", second=" + second +
                '}';
    }

    private List<Integer> judge(int hour, int minute, int second) {
        if (second >= 60) {
            second -= 60;
            minute += 1;
        }
        if (minute >= 60) {
            minute -= 60;
            hour += 1;
        }
        if (hour >= 12) {
            hour -= 12;
        }
        return Arrays.asList(hour,minute,second);
    }
}
